
/**
 *  Copyright 2011 classic-commerce
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package ca.ioniq.server.integration;

import javax.inject.Inject;

import ca.ioniq.server.resource.impl.BrandResourceImpl;
import ca.ioniq.server.resource.impl.CartResourceImpl;
import ca.ioniq.server.resource.impl.CategoryResourceImpl;
import ca.ioniq.server.resource.impl.ItemResourceImpl;
import ca.ioniq.server.resource.impl.OrderResourceImpl;
import ca.ioniq.server.resource.impl.PromotionResourceImpl;
import ca.ioniq.server.resource.impl.UserResourceImpl;

import com.google.inject.AbstractModule;
import com.google.inject.persist.UnitOfWork;

public class ClassicModule extends AbstractModule {
    @Override
    protected void configure() {
        initializePersistenceUnit();
        startPersistenceService();
        bindResources();
    }
 
    private void initializePersistenceUnit() {
        bindConstant().annotatedWith(JpaUnit.class).to("classic-hsqldb-persistence");
        install(PersistenceService.usingJpa()
                .across(UnitOfWork.TRANSACTION)
                .buildModule());
    }
 
    private void startPersistenceService() {
        bind(PersistenceServiceInitializer.class).asEagerSingleton();
    }
 
    private void bindResources() {
	    bind(UserResourceImpl.class);
	    bind(ItemResourceImpl.class);
	    bind(BrandResourceImpl.class);
	    bind(CategoryResourceImpl.class);
	    bind(OrderResourceImpl.class);
	    bind(PromotionResourceImpl.class);
	    bind(CartResourceImpl.class);
    }
    
}

class PersistenceServiceInitializer {
    @Inject
    public PersistenceServiceInitializer(PersistenceService service) {
        service.start();
    }
}
